using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Rtl.SharePoint;
using System.Globalization;


namespace Rtl.SharePoint.WebControls
{
    public class USAddressFieldControl : BaseFieldControl
    {
        #region Globals: Constants and Variables

        private const string PROP_DEF_TMPL = "RtlUSAddressEditRenderingTemplate";

        protected TextBox street1Box = null;
        protected TextBox street2Box = null;
        protected TextBox cityBox = null;
        protected DropDownList stateBox = null;
        protected TextBox zipBox = null;
        protected HiddenField latBox = null;
        protected HiddenField longBox = null;
        protected USAddressFieldValue fieldValue = null;
        protected VEAddressEditor addressEditor = null;

        #endregion

        #region BaseFieldControl: Rendering Method Overrides

        // Returns the ASCX template to use
        protected override string DefaultTemplateName
        {
            get { return PROP_DEF_TMPL; }
        }


        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            USAddressField address = this.Field as USAddressField;

            // If the FieldMetadata Field has been created 
            if (address != null)
            {
                switch (ControlMode)
                {
                    case SPControlMode.Display:
                        // Display mode is handled by a CAML RenderPattern
                        break;

                    case SPControlMode.Edit:
                    case SPControlMode.New:

                        // get template and controls
                        AssociateControlsFromUserControl(TemplateContainer);
                        this.latBox = new HiddenField();
                        this.latBox.ID = "latitudeBox";
                        this.Controls.Add(this.latBox);
                        this.longBox = new HiddenField();
                        this.longBox.ID = "longitudeBox";
                        this.Controls.Add(this.longBox);
                        this.addressEditor.AddressControl = this;

                        // Set Textboxes to Default Custom Properties
                        cityBox.Text = address.DefaultCity;
                        stateBox.SelectedValue = address.DefaultState;
                        break;
                }
            }
        }

        #endregion

        #region BaseFieldControl: Value Methods

        // Uses Field Value object to compress and extract
        // the various decomposible elements of the US Address
        public override object Value
        {
            get
            {
                EnsureChildControls();
                fieldValue = new USAddressFieldValue();
                fieldValue.Street1 = street1Box.Text.Trim();
                fieldValue.Street2 = street2Box.Text.Trim();
                fieldValue.City = cityBox.Text.Trim();
                fieldValue.State = stateBox.SelectedValue.Trim();
                fieldValue.Zip = zipBox.Text.Trim();

                double val;
                if (double.TryParse(latBox.Value, out val))
                    fieldValue.Latitude = val;
                if (double.TryParse(longBox.Value, out val))
                    fieldValue.Longitude = val;

                return fieldValue;
            }
            set
            {
                EnsureChildControls();
                fieldValue = value as USAddressFieldValue;
                street1Box.Text = fieldValue.Street1;
                street2Box.Text = fieldValue.Street2;
                cityBox.Text = fieldValue.City;
                stateBox.SelectedValue = fieldValue.State;
                zipBox.Text = fieldValue.Zip;
                latBox.Value = fieldValue.Latitude.ToString();
                longBox.Value = fieldValue.Longitude.ToString();
            }
        }

        #endregion

        #region USAddressFieldControl: Value Controls

        public TextBox Street1TextBox
        {
            get { EnsureChildControls(); return this.street1Box; ; }
        }

        public TextBox Street2TextBox
        {
            get { EnsureChildControls(); return this.street2Box; }
        }

        public TextBox CityTextBox
        {
            get { EnsureChildControls(); return this.cityBox; }
        }

        public DropDownList StateDropDown
        {
            get { EnsureChildControls(); return this.stateBox; }
        }

        public TextBox ZipTextBox
        {
            get { EnsureChildControls(); return this.zipBox; }
        }

        public HiddenField LatitudeField
        {
            get { EnsureChildControls(); return this.latBox; }
        }

        public HiddenField LongitudeField
        {
            get { EnsureChildControls(); return this.longBox; }
        }

        #endregion

        #region USAddressFieldControl: Rendering Control Helpers

        // Get each textbox from the User Control and
        // associate it with the control's variable
        private void AssociateControlsFromUserControl(TemplateContainer usercontrol)
        {
            street1Box = usercontrol.FindControl("street1Box") as TextBox;
            street1Box.CssClass = this.CssClass;

            street2Box = usercontrol.FindControl("street2Box") as TextBox;
            street2Box.CssClass = this.CssClass;

            cityBox = usercontrol.FindControl("cityBox") as TextBox;
            cityBox.CssClass = this.CssClass;

            stateBox = usercontrol.FindControl("stateBox") as DropDownList;
            stateBox.CssClass = this.CssClass;

            zipBox = usercontrol.FindControl("zipBox") as TextBox;
            zipBox.CssClass = this.CssClass;

            addressEditor = usercontrol.FindControl("addressEditor") as VEAddressEditor;
        }

        public void RegisterDefaultSearchFunction()
        {
            EnsureChildControls();

            string strFormatDefaultSearch = "<script language=\"javascript\">\r\nfunction BuildSearchString_{5}() {{\r\n    var str;\r\n    str = \"\";\r\n\r\n    if (({0} != null) && ({0}.value != null))\r\n        str += {0}.value + \" \";alert(str);\r\n\r\n    if (({1} != null) && ({1}.value != null))\r\n        str += {1}.value + \" \";alert(str);\r\n\r\n    if (({2} != null) && ({2}.value != null))\r\n        str += {2}.value + \" \";alert(str);\r\n\r\n    if (({3} != null) && ({3}.value != null))\r\n        str += {3}.value + \" \";alert(str);\r\n\r\n    if (({4} != null) && ({4}.value != null))\r\n        str += {4}.value;alert(str);\r\n\r\n    return str;   \r\n}}\r\n</script>";
            string strScriptDefaultSearch = string.Format(
                CultureInfo.InvariantCulture,
                strFormatDefaultSearch,
                street1Box != null ? street1Box.ClientID : "nac",
                street2Box != null ? street2Box.ClientID : "nac",
                cityBox != null ? cityBox.ClientID : "nac",
                stateBox != null ? stateBox.ClientID : "nac",
                zipBox != null ? zipBox.ClientID : "nac",
                this.ClientID);

            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "__DefaultSearch__" + this.ClientID, strScriptDefaultSearch);
        }

        #endregion
    }
}